package apis

import (
	"fmt"
	"mmo_game_zinx/core"
	"mmo_game_zinx/pb"
	"yanx/yiface"
	"yanx/ynet"

	"google.golang.org/protobuf/proto"
)

type WorldChatApi struct {
	ynet.BaseRouter
}

func (wc *WorldChatApi) Handle(request yiface.IRequest) {
	// 1 解析客户端传过来的proto协议
	proto_msg := &pb.Talk{}
	err := proto.Unmarshal(request.GetData(), proto_msg)
	if err != nil {
		fmt.Println("Talk Unmarshal error: ", err)
		return
	}

	// 2 获取当前聊天的数据是属于哪个玩家发送的
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("request GetProperty error: ", err)
		return
	}

	// 3根据pid得到对应的player对象
	player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))

	// 4将这个消息广播给其他全部在线的玩家
	player.Talk(proto_msg.Content)
}
